package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class,weight = 100)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    //添加角色
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //将角色信息添加到t_role表
        roleDao.add(role);
        setRoleAndPermission(role.getId(), permissionIds);
        setRoleAndMenu(role.getId(), menuIds);
    }

    //设置角色和权限的关联关系
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                //将role_id和permission_id添加到表t_role_permission
                roleDao.setRoleAndPermission(map);
            }
        }
    }
    //设置角色和菜单的关联关系
    public void setRoleAndMenu(Integer role, Integer[] menuIds) {
        for (Integer menuId : menuIds) {
            Map<String, Integer> map2 = new HashMap<>();
            map2.put("role_id", role);
            map2.put("menu_id", menuId);
            //将role_id和menu_id添加到表t_role_menu
            roleDao.setRoleAndMenu(map2);
        }
    }

    //删除角色
    public void delete(Integer id) {
        //查询当前角色是否和权限关联关联
        long permission = roleDao.findCountByRoleIdp(id);
        long menu = roleDao.findCountByRoleIdm(id);
        if (permission > 0) {
            //当前检查项被引用，删除t_role_permissio
            roleDao.deletepermission(id);
        }
        if (menu > 0) {
            //当前检查项被引用，删除t_role_menu
            roleDao.deletemenu(id);
        }
        roleDao.deleteById(id);
    }

    //编辑
    public void edit(Role role, Integer[] permissionIds,Integer[] menuIds) {
        Integer roleId = role.getId();
        //根据roleId删除t_role_permission表中id
        roleDao.deletepermission(roleId);
        //根据roleId删除t_role_menu表中关联关系
        roleDao.deletemenu(roleId);
        //修改t_role表中关联关系
        roleDao.edit(role);
        setRoleAndPermission(roleId, permissionIds);
        setRoleAndMenu(roleId, menuIds);
    }


    //根据Id查询
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //根据权限查询role_Id
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    //根据菜单查询role_Id
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public List<Permission> findPermissionAll() {
        return roleDao.findPermissionAll();
    }

    @Override
    public List<Menu> findMenuAll() {
        return roleDao.findMenuAll();
    }

    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
